/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package horizon.gui.component;

import horizon.graphics.RenderingContext;
import horizon.util.InputManager;
import horizon.util.Vector2;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;

/**
 *
 * @author J_Laptop
 */
public class GuiWindow extends GuiComponent {

    Color bgColor;
    Color outlineColor;
    Rectangle2D bar;
    Vector2 lastMousePos = new Vector2(0, 0);
    boolean barClicked = false;

    public GuiWindow(String name, Vector2 position, Vector2 size, Color bgColor, Color outlineColor) {
        super(name, size, position);
        this.bgColor = bgColor;
        this.outlineColor = outlineColor;

        bar = new Rectangle(position.getPoint(), new Vector2(size.getX(), size.getY() * .1f).getDim());
    }

    @Override
    public void update(long elapsedTime) {

        MouseEvent drag = InputManager.getInstance().getLastDrag();
//        if (drag != null) {
//            Vector2 v = new Vector2(drag.getX(), drag.getY());
//
//            if (bar.contains(v.getPoint())) {
//
//                lastMousePos = v;
//                barClicked = true;
//
//            } else {
//
//                barClicked = false;
//
//            }
//        }
        if (barClicked) {
            System.out.println("Window Update");



            if (drag != null) {
                System.out.println("Window move");

                Vector2 n = new Vector2(drag.getX(), drag.getY());

                move(n.sub(lastMousePos));

                lastMousePos = n;
                bar = new Rectangle(position.getPoint(), new Vector2(size.getX(), size.getY() * .1f).getDim());
            }

        }
    }

    @Override
    public void render(RenderingContext r) {

        r.drawFilledRectangle(componentBox, bgColor);
        if (inFocus) {
            r.drawRectangle(componentBox, Color.white);
        } else {
            r.drawRectangle(componentBox, outlineColor);
        }
        r.drawFilledRectangle(bar, outlineColor);
    }

    @Override
    public void onActivate(MouseEvent e) {

        Vector2 v = new Vector2(e.getX(), e.getY());

        if (bar.contains(v.getPoint())) {

            lastMousePos = v;
            barClicked = true;

        } else {

            barClicked = false;

        }


    }
}
